package com.itheima.appserver.controller;

import com.itheima.appserver.config.NoAuthorization;
import com.itheima.appserver.service.IMService;
import com.itheima.commons.vo.reception.PageResult;
import com.itheima.commons.vo.reception.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RequestMapping("messages")
@RestController
public class IMController {

    @Autowired
    private IMService imService;


    /**
     * 根据环信用户名查询用户信息
     *
     * @param userName 环信用户  HX_1
     * @return
     */
    @GetMapping("userinfo")
    public ResponseEntity queryUserInfoByUserName(@RequestParam("huanxinId") String userName) {
        UserInfoVo userInfoVo = imService.queryUserInfoByUserName(userName);
        return ResponseEntity.ok(userInfoVo);
    }

    /**
     * 添加好友
     * 当被申请人点击了  聊一下
     *
     * @param param
     * @return
     */
    @PostMapping("contacts")
    public ResponseEntity contactUser(@RequestBody Map<String, Object> param) {
        // 被申请人的ID
        Long friendId = Long.valueOf(param.get("userId").toString());
        this.imService.contactUser(friendId);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询联系人列表
     *
     * @param page
     * @param pageSize
     * @param keyword
     * @return
     */
    @GetMapping("contacts")
    public ResponseEntity queryContactsList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                            @RequestParam(value = "keyword", required = false) String keyword) {
        PageResult pageResult = imService.queryContactsList(page, pageSize, keyword);
        return ResponseEntity.ok(pageResult);
    }


    /**
     * 查询消息点赞列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("likes")
    public ResponseEntity queryLikeCommentList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                               @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        PageResult pageResult = imService.queryCommentList(page, pageSize, 1);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 查询消息评论列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("comments")
    public ResponseEntity queryUserCommentList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                               @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        PageResult pageResult = imService.queryCommentList(page, pageSize, 2);
        return ResponseEntity.ok(pageResult);

    }

    /**
     * 查询消息喜欢列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("loves")
    public ResponseEntity queryLoveCommentList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                               @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        PageResult pageResult = imService.queryCommentList(page, pageSize, 3);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 查询公告列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("announcements")
    @NoAuthorization
    public ResponseEntity<PageResult> queryMessageAnnouncementList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                                   @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        PageResult pageResult = imService.queryMessageAnnouncementList(page, pageSize);
        return ResponseEntity.ok(pageResult);
    }
}
